package com.ahua.mallchat.common.common.event.listener;

import com.ahua.mallchat.common.common.event.UserLoginEvent;
import com.ahua.mallchat.common.common.event.UserRegisterEvent;
import com.ahua.mallchat.common.dao.UserDao;
import com.ahua.mallchat.common.domain.enums.user.IdempotentEnum;
import com.ahua.mallchat.common.domain.enums.user.ItemEnum;
import com.ahua.mallchat.common.domain.enums.user.UserActiveStatusEnum;
import com.ahua.mallchat.common.domain.pojo.User;
import com.ahua.mallchat.common.service.IUserBackpackService;
import com.ahua.mallchat.common.service.IpService;
import com.ahua.mallchat.common.service.impl.IpServiceImpl;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * 用户登录成功事件监听者
 */
@Component
public class UserLoginListener {

    @Resource
    private UserDao userDao;
    @Resource
    private IpService ipService;

    /**
     * 更新用户状态
     */
    @Async
    @EventListener(classes = UserLoginEvent.class)
    public void saveDb(UserLoginEvent event) {
        User user = event.getUser();

        //更新到数据库
        User update = new User();
        update.setId(user.getId());
        update.setLastOptTime(user.getLastOptTime());
        update.setActiveStatus(UserActiveStatusEnum.ONLINE.getStatus());
        update.setIpInfo(user.getIpInfo());
        userDao.updateById(update);
        //用户ip的详情解析
        ipService.refreshUserIpDetailAsync(user.getId());
    }
}
